package com.huaqin.punan.sliding;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;

public class MyMoveView extends ViewGroup {

	public static final String TAG = "MoveViewLog";
	
	private final static int MOVE_TO_LEFT = 1;
	private final static int MOVE_TO_RIGHT = 2;
	private final static int MOVE_TO_REST = 0;

	public final static int MAIN = 0;
	public final static int LEFT = 1;
	public final static int RIGHT = 2;

	private int move_state = MOVE_TO_REST;

	private int now_state = MAIN;

	private final float WIDTH_RATE = 0.83f;
	private BaseView main_show_view;
	private LeftMenuView left_show_view;
	private Context context;

	private int min_distance = 200;
	private final int MOVE_TIME = 1;// 移动时间
	private int move_main = 90;// 移动距离
	private int move_left = 40;// 移动的最后距离

	private int screen_w;
	private int screen_h;

	private int move_x_v;

	private boolean isFaceRuturn = true; // 是否界面返回
	private boolean isStarted = false; // 是否刚启动,false:一进来显示左边,true,显示中间

	private boolean isAimationMoving = false;
	private boolean isMoveEnable = true;

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			synchronized (MyMoveView.this) {
				isAimationMoving = true;
				int move_change = (int) (screen_w * WIDTH_RATE / 6);
				int left = (int)(screen_w - main_show_view.getView().getRight()); //modified
				if (msg.what == 1) {
					move(move_change + left);
				}
				if (msg.what == 11) {
					isAimationMoving = false;
					moveToLeft(false);
				}
				if (msg.what == 2) {
					move(-1 * move_change + left);
				}

				if (msg.what == 0) {
					if (now_state == LEFT) {
						move(move_x_v);
					} else {
						move(move_x_v);
					}
				}
				if (msg.what == 10) {
					isAimationMoving = false;
					moveToMain(false, 0);
				}
			}
		}
	};

	public MyMoveView(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	public MyMoveView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
	}

	public MyMoveView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initView();
	}

	public void initView() {
		Display display = ((Activity) context).getWindowManager()
				.getDefaultDisplay();
		int width = display.getWidth();
		int Height = display.getHeight();
		this.screen_w = width;
		this.screen_h = Height;
		this.move_main = getWindowWidth(context) / 5;
		this.move_left = getWindowWidth(context) / 5;
		this.setKeepScreenOn(true);
		this.min_distance = (int) (screen_w / 5.0);
	}

	public void setMainView(BaseView mainView, LeftMenuView leftView, int curTab) {
//		this.currentTab = curTab;
		if (this.left_show_view == null) {
			this.left_show_view = leftView;
			this.addView(left_show_view.getView());
		}

		if (this.main_show_view == null) {
			this.addView(mainView.view);
		} else {
			this.removeView(this.main_show_view.view);
			this.addView(mainView.view);
		}
		this.main_show_view = mainView;
		if (isStarted) {
			moveToMain(isFaceRuturn, 0);
		} else {
			isStarted = true;
		}

		if (!isFaceRuturn) {
			isFaceRuturn = true;
		}
	}

	public void move(int start) {
		int left = (int)(screen_w - main_show_view.getView().getRight());//modified
		
		Log.e(TAG, "move start="+start +",left="+left);
		
		if (now_state == MAIN) {
			if (left > 0) {
				if (move_state != MOVE_TO_LEFT) {
					move_state = MOVE_TO_LEFT;
				}
				left_show_view.getView().setVisibility(View.VISIBLE);
			} else if (left < 0) {
				if (move_state != MOVE_TO_RIGHT) {
					move_state = MOVE_TO_RIGHT;
				}
				left_show_view.getView().setVisibility(View.VISIBLE);
			} else {
				move_state = MOVE_TO_REST;
			}
			main_show_view.getView().layout(-start, 0, screen_w - start, screen_h);
		} else {
			left = (int) (screen_w * WIDTH_RATE);
			if (now_state == RIGHT) {
				left = -1 * left;
			}
			left = -left + start;
			main_show_view.getView().layout(left, 0, left + screen_w, screen_h);
		}
	}

	@Override
	protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
		if (move_state == MOVE_TO_REST) {
			if (now_state == MAIN) {
				int w = (int) (screen_w - screen_w * WIDTH_RATE); //modified
				main_show_view.getView().layout(0, 0, screen_w, screen_h);
				left_show_view.getView().layout(w, 0, screen_w, screen_h); //modified

			} else if (now_state == LEFT) {
				moveToLeft(false);
			}
		}
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		main_show_view.view.measure(widthMeasureSpec, heightMeasureSpec);
		left_show_view.getView().measure(MeasureSpec.UNSPECIFIED,
				heightMeasureSpec);
		left_show_view.setWidth((int) (screen_w * WIDTH_RATE));
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	private boolean isMoved;

	public boolean getIsMoved() {
		return isMoved;
	}

	public void moveToLeft(boolean b) {
		if (!b) {
			int move_x = (int) (screen_w * WIDTH_RATE);
			int left_menu_x = (int) (screen_w - screen_w * WIDTH_RATE); //modified
			left_show_view.getView().layout(left_menu_x, 0, screen_w, screen_h); //modified
			main_show_view.getView().layout(-move_x, 0, left_menu_x, screen_h); //modified

			now_state = LEFT;

		} else {
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					int left = (int) (main_show_view.view.getRight());//modified
					
					Message msg = new Message();
					if (left > move_left) {
						msg.what = 1;
						mHandler.sendMessage(msg);
						mHandler.postDelayed(this, MOVE_TIME);
					} else {
						msg.what = 11;
						mHandler.sendMessage(msg);
					}
				}
			}, 0);
		}
	}

	public void moveToMain(boolean b, int move) {
		if (!b) {
			left_show_view.getView().setVisibility(View.VISIBLE);
			int w = (int) (screen_w - screen_w * WIDTH_RATE); //modified
			main_show_view.getView().layout(0, 0, screen_w, screen_h);
			left_show_view.getView().layout(w, 0, screen_w, screen_h);

			now_state = MAIN;

		} else {
			move_x_v = move;
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					int left = (int)(screen_w - main_show_view.view.getRight()); //modified

					Message msg = new Message();
					if (left > move_main) {
						msg.what = 0;
						move_x_v = move_x_v + move_main;
						now_state = LEFT;
						mHandler.sendMessage(msg);
						mHandler.postDelayed(this, MOVE_TIME);
					} else {
						msg.what = 10;
						mHandler.sendMessage(msg);
					}
				}
			}, 0);
		}
	}

	public int getNowState() {
		return this.now_state;
	}

	public void setNowState(int state) {
		this.now_state = state;
	}

	public LeftMenuView getLeft_show_view() {
		return left_show_view;
	}

	public BaseView getMain_show_view() {
		return main_show_view;
	}

	/**
	 * 显示隐藏左边菜单
	 */
	public void showHideLeftMenu() {
		if (now_state == MAIN) {
			moveToLeft(true);
		} else {
			moveToMain(true, 0);
		}
	}

	public boolean isMoveEnable() {
		return isMoveEnable;
	}

	public void setMoveEnable(boolean isMoveEnable) {
		this.isMoveEnable = isMoveEnable;
	}

	public void setFaceRuturn(boolean isFaceRuturn) {
		this.isFaceRuturn = isFaceRuturn;
	}

	public int getWindowWidth(Context context) {
		DisplayMetrics dm = new android.util.DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getWindowHeight(Context context) {
		DisplayMetrics dm = new android.util.DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.heightPixels;
	}
	
}
